/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solitaireofdoom.klondike;

import java.awt.*;
import java.awt.image.*;

import solitaireofdoom.solitaire.*;

/**
 * The KlondikeDrawPile and KlondikeDonePile both inherit the Pile class and serve basic pile functions. 
 *
 * @author Administrator
 */
public class KlondikeDrawPile extends Pile
{
    private KlondikeDonePile donePile;
    private Klondike klondike;

    public KlondikeDrawPile(KlondikeDonePile donePile, Klondike klondike, BufferedImage cards)
    {
        super();

        this.cards = cards;
        this.donePile = donePile;
        this.klondike = klondike;

        try
        {
            CardCanvas cc = new CardCanvas(new Card('H', 998, true), cards);
            this.add(cc);
        }
        catch (Exception e) {  }
    }

    public KlondikeDrawPile(KlondikeDonePile donePile, Deck deck, Klondike klondike, BufferedImage cards)
    {
        super();

        this.cards = cards;
        this.donePile = donePile;
        this.klondike = klondike;

        try
        {
            CardCanvas cc = new CardCanvas(new Card('H', 998, true), cards);
            this.add(cc);
        }
        catch (Exception e) {  }

        for (int i = 0; i < deck.size(); i++)
        {
            CardCanvas cc = new CardCanvas(deck.getCard(i), cards);
            this.add(cc);
        }
    }

    /**
     * Adds a <b>CardCanvas</b> to this pile.
     * @param cc the <b>CardCanvas</b> that is supposed to be added to this pile.
     * @return returns the new <b>CardCanvas</b>
     */
    public java.awt.Component add(CardCanvas cc)
    {
        cc.pile = this;
        this.pileCards.add(cc);

        this.redraw();

        return cc;
    }

    /**
     * Removes all the cards from this pile
     */
    public void clear()
    {
        this.pileCards.clear();

        try
        {
            CardCanvas cc = new CardCanvas(new Card('H', 998, true), cards);
            this.add(cc);
        }
        catch (Exception e) {  }

        this.redraw();
    }

    /**
     * Shows the cards in the pile
     */
    public void redraw()
    {
        this.removeAll();

        if (this.pileCards.size() > 1)
        {
            for (int i = this.pileCards.size() - 1; i >= 1 && i >= this.pileCards.size() - 4; i--)
            {
                int x = 1 + 4 * (3 - (this.pileCards.size() - i - 1));
                this.pileCards.elementAt(i).setLocation(x, 1);
                this.add((Component)this.pileCards.elementAt(i));
            }
        }
        else
        {
            this.pileCards.elementAt(0).setLocation(13, 1);
            this.add((Component)this.pileCards.elementAt(0));
        }
    }

    /**
     * Reports if a card was clicked in the pile
     *
     * @param isTopMost is the card that was click on top
     */
    public void cardClicked(boolean isTopMost)
    {
        if (isTopMost && this.pileCards.size() > 1)
        {
            this.donePile.add(this.pileCards.lastElement());
            this.pileCards.remove(this.pileCards.lastElement());
            this.redraw();

            this.klondike.pileChanged(false);
        }
        else if (this.pileCards.size() == 1 && this.donePile.pileCards.size() > 0)
        {
            for (int i = this.donePile.pileCards.size() - 1; i >= 0; i--)
            {
                CardCanvas cc = this.donePile.pileCards.elementAt(i);
                cc.getCard().setFacing(false);
                cc.pile = this;
                this.pileCards.add(cc);
                this.donePile.pileCards.removeElementAt(i);
            }
            
            this.donePile.redraw();
            this.redraw();

            this.pileCards.lastElement().getCard().setFacing(true);
            this.cardClicked(true);

            this.klondike.pileChanged(true);
        }
    }

    /**
     * Moves cards to the suit piles(foundations) where applicable
     */
    public void autoMoveToSuit()
    {
        this.klondike.autoMoveToSuit();
    }
}
